<?php
namespace app\admin\controller;
use think\Db;
use think\Cache;
use app\common\controller\AdminBaseController;

/*
* 网站日志
*/
class WebLogController extends AdminBaseController {
	
	public function index(){
		$methods = ['GET','POST','PUT','DELETE','HEAD','PATCH','OPTIONS','Ajax','Pjax'];
		$request_module = input('request_module','');
		$controllers = array();
		$controllers_arr = array();
		if($request_module){
			$controllers_arr = \ReadClass::readDir(APP_PATH . $request_module. DS .'controller');
			$controllers = array_keys($controllers_arr);
		}
		$request_controller = input('request_controller','');
		$actions=array();
		if($request_module && $request_controller){
			$actions = $controllers_arr[$request_controller];
			$actions = array_map('array_shift',$actions['method']);
		}
		$request_action = input('request_action','');
		$request_method = input('request_method','');
		//组成where
		$where=array();
		if($request_module){
			$where['module'] = $request_module;
		}
		if($request_controller){
			$where['controller'] = $request_controller;
		}
		if($request_action){
			$where['action'] = $request_action;
		}
		if($request_method){
			$where['method'] = $request_method;
		}
		$weblog_list = Db::name('web_log')->where($where)->order('otime desc')->paginate(15,false,['query'=>get_query()]);
		$show = $weblog_list->render();
		$show = preg_replace("(<a[^>]*page[=|/](\d+).+?>(.+?)<\/a>)","<a href='javascript:ajax_page($1);'>$2</a>",$show);
		$this->assign('weblog_list',$weblog_list);
		$this->assign('page',$show);
		$this->assign('request_module',$request_module);
		$this->assign('request_controller',$request_controller);
		$this->assign('request_action',$request_action);
		$this->assign('request_method',$request_method);
		$this->assign('controllers',$controllers);
		$this->assign('actions',$actions);
		$this->assign('methods',$methods);
		return $this->fetch();
	}

	public function delete($id, $ids = []){
		$id = $ids ? $ids : $id;
        if (Db::name('web_log')->delete($id)) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
	}
	
	public function drop(){
		$rst=Db::name('web_log')->where('id','gt',0)->delete();
		if($rst!==false){
			$this->success('清空成功');
		}else{
			$this->error("清空失败");
		}
	}

    	
}
